import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)


// 持久化插件函数
const persistPlugin = (store) => {
	// 初始化时从本地读取状态
	const savedState = getStorageSync('vuex_state')
	if (savedState) {
		store.replaceState(savedState)
	}

	// 每次状态变更时保存到本地
	store.subscribe((mutation, state) => {
		setStorageSync('vuex_state', state)
	})
}

function getStorageSync(key) {
	try {
		const value = uni.getStorageSync(key)
		return value ? JSON.parse(value) : null
	} catch (e) {
		console.error('getStorageSync error:', e)
		return null
	}
}

function setStorageSync(key, data) {
	try {
		uni.setStorageSync(key, JSON.stringify(data))
	} catch (e) {
		console.error('setStorageSync error:', e)
	}
}

const store = new Vuex.Store({
	state: {
		isLogin: uni.getStorageSync('isLogin') || false, //是否登录
		userInfo: uni.getStorageSync('userInfo') || {},
		appVersion: '1.0.0',
		shareId: ''
	},
	mutations: {
		setShareId(state, shareId) {
			state.shareId = shareId;
		},
		setAppVersion(state, appVersion) {
			state.appVersion = appVersion;
		},
		setUserId(state, userId) {
			state.userInfo.userId = userId;
		},
		setNickName(state, nickName) {
			state.userInfo.nickName = nickName;
		},
		setUserTele(state, userTele) {
			state.userInfo.userTele = userTele;
		},
		setAvatar(state, avatar) {
			state.userInfo.avatar = avatar;
		},
		setUserSex(state, userSex) {
			state.userInfo.userSex = sex;
		},
		setMemberType(state, memberType) {
			state.userInfo.memberType = memberType;
		},
		setIsLogin(state, isLogin) {
			state.isLogin = isLogin;
		},
		setToken(state, token) {
			state.userInfo.token = token;
		},
		setUserScore(state, userScore) {
			state.userInfo.userScore = userScore;
		},
		setUserInfo(state, data) {
			console.log("开始缓存用户信息 ==> {}", data);
			state.isLogin = true
			if (data.id) {
				state.userInfo.userId = data.id
			}
			if (data.nickName) {
				state.userInfo.nickName = data.nickName
			}
			if (data.realName) {
				state.userInfo.realName = data.realName
			}
			if (data.tele) {
				state.userInfo.userTele = data.tele
			}
			if (data.avatar) {
				state.userInfo.avatar = data.avatar
			}
			if (data.sex) {
				state.userInfo.userSex = data.sex
			}
			if (data.memberType) {
				state.userInfo.memberType = data.memberType
			}
			if (data.token) {
				state.userInfo.token = data.token
			}
			uni.setStorageSync('isLogin', state.isLogin);
			uni.setStorageSync('userInfo', state.userInfo);
		},
		setLogout(state, value) {
			state.isLogin = false
			state.userInfo = value
			uni.setStorageSync('isLogin', false);
			uni.setStorageSync('userInfo', {});
		}
	},
	actions: {

	},
	getters: {
		getShareId(state, getters) {
			return state.shareId
		},
		getAppVersion(state, getters) {
			return state.appVersion
		},
		getUserInfo(state, getters) {
			return state.userInfo
		},
		getUserId(state, getters) {
			return state.userInfo.userId
		},
		getNickName(state, getters) {
			return state.userInfo.nickName
		},
		getUserTele(state, getters) {
			return state.userInfo.userTele
		},
		getAvatar(state, getters) {
			return state.userInfo.avatar
		},
		getUserSex(state, getters) {
			return state.userInfo.userSex
		},
		getMemberType(state, getters) {
			return state.userInfo.memberType
		},
		getIsLogin(state, getters) {
			return state.isLogin
		},
		getTokenName(state, getters) {
			return state.userInfo.tokenName
		},
		getTokenValue(state, getters) {
			return state.userInfo.tokenValue
		},
		getUserScore(state, getters) {
			return state.userInfo.userScore
		},
		getUserName(state, getters) {
			return state.userInfo.realName ? state.userInfo.realName : state.userInfo.nickName
		}
	},
	plugins: [persistPlugin]
})
export default store